<template>
  <div class="app-container">
    <el-row gutter="20">
      <el-col span="4">
        <el-input
          v-model="deptName"
          size="small"
          placeholder="请输入部门名称"
          prefix-icon="el-icon-search"
        />
        <el-tree
          ref="tree"
          class="tree-top"
          :data="data"
          :props="defaultProps"
          highlight-current
          :expand-on-click-node="false"
          default-expand-all="true"
          node-key="id"
          @node-click="handleNodeClick"
        />
      </el-col>
      <el-col span="20">
        <el-form v-if="show" ref="params" v-permission="['sys:user:page']" :model="params" :inline="true" class="search-form">
          <el-form-item label="用户名称" prop="userName">
            <el-input v-model="params.userName" />
          </el-form-item>
          <el-form-item label="手机号码" prop="phone">
            <el-input v-model="params.phone" />
          </el-form-item>
          <el-form-item label="状态" prop="state">
            <el-select v-model="params.state" clearable placeholder="请选择">
              <el-option
                v-for="item in options"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="创建时间" prop="createTime">
            <el-date-picker
              v-model="params.createTime"
              type="daterange"
              value-format="yyyy-MM-dd HH:mm:ss"
              range-separator="至"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
            />
          </el-form-item>
          <el-form-item>
            <el-button type="primary" icon="el-icon-search" @click="findPage">搜索</el-button>
            <el-button icon="el-icon-refresh" @click="resetForm('params')">重置</el-button>
          </el-form-item>
        </el-form>
        <el-row class="btn-group" type="flex" justify="space-between">
          <el-col span="20">
            <el-button
              v-permission="['sys:user:save']"
              type="primary"
              icon="el-icon-plus"
              @click="add"
            >新增</el-button>
            <el-button
              v-permission="['sys:user:update']"
              type="success"
              icon="el-icon-edit"
              :disabled="multipleSelection.length!==1"
              @click="edit"
            >修改</el-button>
            <el-button
              v-permission="['sys:user:deleteIds']"
              type="danger"
              icon="el-icon-delete"
              :disabled="multipleSelection.length===0"
              @click="remove"
            >删除</el-button>
            <el-button v-permission="['sys:user:import']" type="info" icon="el-icon-upload">导入</el-button>
            <el-button v-permission="['sys:user:export']" type="warning" icon="el-icon-download">导出</el-button>
          </el-col>
          <el-col span="4" align="right">
            <el-tooltip class="item" effect="dark" content="显示搜索" placement="top">
              <el-button icon="el-icon-search" circle @click="show = !show" />
            </el-tooltip>
            <el-tooltip class="item" effect="dark" content="刷新" placement="top">
              <el-button icon="el-icon-refresh" circle @click="findPage" />
            </el-tooltip>
          </el-col>
        </el-row>
        <el-table
          ref="multipleTable"
          :header-cell-style="{background:'#eef1f6',color:'#606266'}"
          :data="users"
          tooltip-effect="dark"
          style="width: 100%"
          @selection-change="handleSelectionChange"
        >
          <el-table-column
            type="selection"
            width="55"
            align="center"
          />
          <el-table-column
            label="序号"
            type="index"
            width="50"
            align="center"
          />
          <el-table-column
            prop="userName"
            label="用户名称"
            width="120"
            align="center"
          />
          <el-table-column
            prop="nickName"
            label="昵称"
            width="120"
            align="center"
          />
          <el-table-column
            prop="phone"
            label="手机号码"
            width="120"
            align="center"
          />
          <el-table-column
            prop="deptName"
            label="部门"
            width="120"
            align="center"
          />
          <el-table-column prop="state" label="状态" align="center" width="100">
            <template slot-scope="{row}">
              <el-switch v-model="row.state" />
            </template>
          </el-table-column>
          <el-table-column
            prop="createTime"
            label="创建时间"
            align="center"
            width="160"
          />
          <el-table-column label="操作" min-width="200px" align="center">
            <template slot-scope="{row}">
              <div>
                <el-button v-permission="['sys:user:update']" type="text" icon="el-icon-edit" @click="edit(row.id)">修改</el-button>
                <el-button v-permission="['sys:user:delete']" type="text" icon="el-icon-delete" @click="remove(row.id)">删除</el-button>
                <el-button v-permission="['sys:user:resetPassword']" type="text" icon="el-icon-key" @click="resetPassword(row.id)">重置</el-button>
              </div>
            </template>
          </el-table-column>
        </el-table>
        <el-pagination
          v-if="total"
          class="table—page"
          :current-page="page.current"
          :page-sizes="[10, 20, 30, 50,100]"
          :page-size="page.size"
          background
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </el-col>
    </el-row>
    <user-form :id.sync="id" :title="title" :show.sync="dialogShow" :tree="data" />
  </div>
</template>

<script>
import { findPage, remove, removeIds } from '@/api/sys/user'
import { tree } from '@/api/sys/dept'
import UserForm from './components/userForm'

export default {
  name: 'User',
  components: { UserForm },
  data() {
    return {
      id: null,
      deptId: null,
      userName: '',
      deptName: '',
      data: [],
      defaultProps: {
        children: 'children',
        label: 'label'
      },
      users: [],
      options: [{ label: '停用', value: false }, { label: '正常', value: true }],
      show: true,
      page: { 'current': 1, 'size': 10 },
      params: { userName: '', phone: '', state: null, createTime: null },
      total: 0,
      title: '添加用户',
      dialogShow: false,
      checkedNodes: [],
      multipleSelection: []
    }
  },
  async created() {
    await this.findTree()
    await this.findPage()
  },
  mounted() {
  },
  methods: {
    handleNodeClick(data) {
      this.deptId = data.id
      this.page = { 'current': 1, 'size': 10 }
      this.findPage()
    },
    handleSelectionChange(val) {
      this.multipleSelection = val
    },
    handleSizeChange(val) {
      this.page.size = val
      this.findPage()
    },
    handleCurrentChange(val) {
      this.page.current = val
      this.findPage()
    },
    resetForm(formName) {
      this.$refs[formName].resetFields()
    },
    async findTree() {
      const res = await tree()
      this.data = res.data
      this.deptId = this.data[0].id
      this.$nextTick(() => {
        this.$refs.tree.setCurrentKey(this.deptId)
      })
    },
    async findPage() {
      const page = { ...this.page }
      const params = { ...this.params }
      params.deptId = this.deptId
      if (params.createTime) {
        params.startTime = params.createTime[0]
        params.endTime = params.createTime[1]
      }
      delete params.createTime
      const res = await findPage(page, params)
      this.users = res.data.records
      this.total = res.data.total
    },
    add() {
      this.title = '添加用户'
      this.dialogShow = true
    },
    edit(id) {
      this.title = '修改用户'
      this.dialogShow = true
      this.id = typeof id === 'number' ? id : this.multipleSelection[0].id
    },
    remove(id) {
      this.$confirm('此操作将删除该记录, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        if (typeof id === 'number') {
          await remove(id)
          await this.findPage()
        } else {
          const ids = []
          this.multipleSelection.map(val => {
            ids.push(val.id)
          })
          await removeIds(ids)
          await this.findPage()
        }
      })
    },
    resetPassword(id) {

    }
  }
}
</script>

<style scoped>
.tree-top {
  margin-top: 20px;
}
</style>
